﻿using DataAccess;
using DataEntity;
using IntelligentQuery.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebUI.Controllers
{
    public class RoleController : Controller
    {
        //
        // GET: /Role/

        private DataContext db = new DataContext();
        private IEntityRepository<SysRole> RoleRepository;
        public RoleController()
        {
            RoleRepository = new EntityRepository<SysRole>(db);
        }
        #region 页面
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 新建/编辑页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult CreateEdit(int id = 0)
        {
            var model = new SysRole();

            if (id != 0)
            {
                model = RoleRepository.GetByKey(id);
            }

            return PartialView(model);
        }
        #endregion

        #region  json返回数据
        /// <summary>
        /// 角色信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult RoleJson(int? page, int? rows, string model)
        {
            int Page = page ?? 1;
            int Rows = rows ?? 10;
            QueryModel QModel = null;
            Dictionary<string, object> JsonObj = new Dictionary<string, object>();
            if (!string.IsNullOrWhiteSpace(model))
            {
                QModel = IntelligentQuery.AjaxQueryConverter.ToQueryModel(model);
            }
            var rolelist = RoleRepository.Get(QModel).Where(n =>!n.BeLock).ToList();

            JsonObj.Add("total", rolelist.Count());
            JsonObj.Add("rows", rolelist.OrderBy(p => p.ID)
                    .Skip((Page - 1) * Rows)
                   .Take(Rows).Select(s => new { s.ID, s.Code, s.Name, s.Remark, s.BeLock }));

            return JsonHelper.JsonEx(JsonObj);
        }


        public ActionResult RoleList()
        {
            List<object> objList = new List<object>();
            var rolelist = db.SysRoles.Where(n => n.BeLock == false).ToList();
            foreach (var role in rolelist)
            {
                objList.Add(new { id = role.ID, text = role.Name });
           }
            return Json(objList);
        }
        #endregion

        #region  操作方法
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(SysRole model)
        {
            try
            {
                if (model.ID == 0)
                {

                    RoleRepository.Insert(model);
                }
                else
                {
                    var role = RoleRepository.GetByKey(model.ID);
                    role.Remark = model.Remark;
                    role.Name = model.Name;
                    role.Code = model.Code;

                    RoleRepository.Update(role);

                }


                return Json(new { success = true });
            }
            catch (Exception e)
            {
                return Json(new { msg = e.Message });
            }
        }
        /// <summary>
        /// 单个删除 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult SingleDelete(int id)
        {
            try
            {
                var model = RoleRepository.GetByKey(id);
                model.BeLock = true;
                RoleRepository.Update(model);

                return Json(new { success = true });
            }
            catch (Exception e)
            {
                return Json(new { msg = e.Message });
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult BatchDelete(string ids)
        {
            try
            {
                List<string> list_id = ids.Split(',').ToList();
                var roleList = RoleRepository.Get().ToList().Where(u => list_id.Contains(u.ID.ToString())).ToList();
                foreach (var role in roleList)
                {
                    role.BeLock = true;

                }
                RoleRepository.Update(roleList);
                return Json(new { success = true });
            }
            catch (Exception e)
            {
                return Json(new { msg = e.Message });
            }
        }
       #endregion
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
